package com.website.main.controller;

import com.website.main.service.S_Power;
import com.website.mybatis.entity.SysPower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin/power")
public class C_Power {

	@Autowired
	S_Power power;

	/**
	 * 获取所有
	 * @return
	 */
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String,Object> getAll(Boolean checked){
		return power.getAll(checked);
	}
	/**
	 * 获取所有
	 * @return
	 */
	@RequestMapping("/getByRole")
	@ResponseBody
	public Map<String,Object> getByRole(@RequestParam(required=true)Integer roleId){
		return power.getByRole(roleId);
	}

	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(SysPower p){
		return power.save(p);
	}
	@RequestMapping("/update")
	@ResponseBody
	public Map<String,Object> update(SysPower p){
		return power.update(p);
	}
	@RequestMapping("/getById")
	@ResponseBody
	public Map<String,Object> getById(Integer id){
		return power.getById(id);
	}
	@RequestMapping("/del")
	@ResponseBody
	public Map<String,Object> del(@RequestParam(required=true)Integer id){
		try {
			return power.delete(id);
		} catch (DataIntegrityViolationException e) {
			Map<String,Object> map = new HashMap<>();
			map.put("success", false);
			map.put("msg", "删除失败，尚有角色关联此权限");
			return map;
		}
	}
}
